package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.annotation.LoginAnnotation;
import cn.wolfcode.luowowo.annotation.UserParam;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.domain.Destination;
import cn.wolfcode.luowowo.domain.Travel;
import cn.wolfcode.luowowo.domain.TravelComment;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.query.TravelQuery;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.ITravelCommentService;
import cn.wolfcode.luowowo.service.ITravelService;
import cn.wolfcode.luowowo.util.JsonResult;
import cn.wolfcode.luowowo.util.UMEditorUploader;
import org.apache.catalina.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("travels")
public class TravelController {


    @Autowired
    private ITravelService travelService;
    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private IUserInfoRedisService userInfoRedisService;

    @Autowired
    private ITravelCommentService travelCommentService;

    @GetMapping("/query")
    public Object query(TravelQuery qo){

        Page<Travel> query = travelService.query(qo);
//        System.err.println(query);
        return JsonResult.success(query);
    }

    @GetMapping("/input")
    public Object input(String id){
        Map<String,Object> map = new HashMap<>();
        if (StringUtils.hasLength(id)) {
            Travel travel = travelService.get(id);
            map.put("tv",travel);
        }
        List<Destination> list = destinationService.list();
        map.put("dests",list);
        return JsonResult.success(map);
    }



    @LoginAnnotation
    @PostMapping("/saveOrUpdate")
    public Object saveOrUpdate(Travel travel,HttpServletRequest request){

        String token = request.getHeader("token");
        UserInfo userInfo = userInfoRedisService.getUserInfo(token);
        travel.setUserId(userInfo.getId());

        travelService.saveOrUpdate(travel);

        return JsonResult.success(travel.getId());
    }


    @GetMapping("/detail")
    public Object detail(String id){
        Travel travel = travelService.get(id);
        return JsonResult.success(travel);
    }

    @LoginAnnotation
    @PostMapping("/commentAdd")
    public Object commentAdd(TravelComment comment, @UserParam UserInfo user){

        BeanUtils.copyProperties(user,comment);
        comment.setUserId(user.getId());
        travelCommentService.commentAdd(comment);

        return JsonResult.success();
    }


    @GetMapping("/comments")
    public Object comments(String travelId){
        List<TravelComment> comments = travelCommentService.comments(travelId);
        return JsonResult.success(comments);
    }



















    //上传图片
    @RequestMapping("/contentImage")
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception{
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\""+ up.getFileName() +"\", \"originalName\": \""+ up.getOriginalName() +"\", \"size\": "+ up.getSize()
                +", \"state\": \""+ up.getState() +"\", \"type\": \""+ up.getType() +"\", \"url\": \""+ up.getUrl() +"\"}";
        result = result.replaceAll( "\\\\", "\\\\" );
        if(callback == null ){
            return result ;
        }else{
            return "<script>"+ callback +"(" + result + ")</script>";
        }
    }


}
